home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / tcl-mode.el < prev    next >
Lisp/Scheme  |  1996-07-12  |  20KB  |  665 lines

  1. ;;; tcl-mode.el --- a major-mode for editing tcl/tk scripts
  2.  
  3. ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Gregor Schmid <schmid@fb3-s7.math.tu-berlin.de>
  6. ;; Keywords: languages, processes, tools
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Special Thanks to Simon Marshall <simonm@mail.esrin.esa.it> for
  28. ;; font-lock patches.
  29.  
  30. ;; This file was written with emacs using Jamie Lokier's folding mode
  31. ;; That's what the funny ;;{{{ marks are there for
  32.  
  33. ;;{{{ Usage
  34.  
  35. ;; Tcl-mode supports c-mode style formatting and sending of
  36. ;; lines/regions/files to a tcl interpreter. An interpreter (see
  37. ;; variable `tcl-default-application') will be started if you try to
  38. ;; send some code and none is running. You can use the process-buffer
  39. ;; (named after the application you chose) as if it were an
  40. ;; interactive shell. See the documentation for `comint.el' for
  41. ;; details.
  42.  
  43. ;;}}}
  44. ;;{{{ Key-bindings
  45.  
  46. ;; To see all the keybindings for folding mode, look at `tcl-setup-keymap'
  47. ;; or start `tcl-mode' and type `\C-h m'.
  48. ;; The keybindings may seem strange, since I prefer to use them with
  49. ;; tcl-prefix-key set to nil, but since those keybindings are already used
  50. ;; the default for `tcl-prefix-key' is `\C-c', which is the conventional
  51. ;; prefix for major-mode commands.
  52.  
  53. ;; You can customise the keybindings either by setting `tcl-prefix-key'
  54. ;; or by putting the following in your .emacs
  55. ;;     (setq tcl-mode-map (make-sparse-keymap))
  56. ;; and
  57. ;;     (define-key tcl-mode-map <your-key> <function>)
  58. ;; for all the functions you need.
  59.  
  60. ;;}}}
  61. ;;{{{ Variables
  62.  
  63. ;; You may want to customize the following variables:
  64. ;;     tcl-indent-level
  65. ;;     tcl-always-show
  66. ;;    tcl-mode-map
  67. ;;    tcl-prefix-key
  68. ;;    tcl-mode-hook
  69. ;;     tcl-default-application
  70. ;;     tcl-default-command-switches
  71.  
  72. ;;}}}
  73.  
  74. ;;; Code:
  75.  
  76. ;; We need that !
  77. (require 'comint)
  78.  
  79. ;;{{{ variables
  80.  
  81. (defvar tcl-default-application "wish"
  82.   "Default tcl/tk application to run in tcl subprocess.")
  83.  
  84. (defvar tcl-default-command-switches nil
  85.   "Command switches for `tcl-default-application'.
  86. Should be a list of strings.")
  87.  
  88. (defvar tcl-process nil
  89.   "The active tcl subprocess corresponding to current buffer.")
  90.  
  91. (defvar tcl-process-buffer nil
  92.   "Buffer used for communication with tcl subprocess for current buffer.")
  93.  
  94. (defvar tcl-always-show t
  95.   "*Non-nil means display tcl-process-buffer after sending a command.")
  96.  
  97. (defvar tcl-mode-map nil
  98.   "Keymap used with tcl mode.")
  99.  
  100. (defvar tcl-prefix-key "\C-c"
  101.   "Prefix for all tcl-mode commands.")
  102.  
  103. (defvar tcl-mode-hook nil
  104.   "Hooks called when tcl mode fires up.")
  105.  
  106. (defvar tcl-region-start (make-marker)
  107.   "Start of special region for tcl communication.")
  108.  
  109. (defvar tcl-region-end (make-marker)
  110.   "End of special region for tcl communication.")
  111.  
  112. (defvar tcl-indent-level 4
  113.   "Amount by which tcl subexpressions are indented.")
  114.  
  115. (defvar tcl-default-eval "eval"
  116.   "Default command used when sending regions.")
  117.  
  118. (defvar tcl-mode-menu (make-sparse-keymap "Tcl-Mode")
  119.   "Keymap for tcl-mode's menu.")
  120.  
  121. (defvar tcl-font-lock-keywords
  122.   (eval-when-compile
  123.     (list
  124.      ;;
  125.      ;; Function name declarations.
  126.      '("\\<\\(itcl_class\\|method\\|proc\\)\\>[ \t]*\\(\\sw+\\)?"
  127.        (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  128.      ;;
  129.      ;; Keywords.
  130. ;(make-regexp '("if" "then" "else" "elseif" "for" "foreach" "break"
  131. ;           "continue" "while" "eval" "case" "in" "switch" "default"
  132. ;           "exit" "error" "proc" "return" "uplevel" "constructor"
  133. ;           "destructor" "itcl_class" "loop" "for_array_keys"
  134. ;           "for_recursive_glob" "for_file"))
  135.      (concat "\\<\\("
  136.          "break\\|c\\(ase\\|on\\(structor\\|tinue\\)\\)\\|"
  137.          "de\\(fault\\|structor\\)\\|"
  138.          "e\\(lse\\(\\|if\\)\\|rror\\|val\\|xit\\)\\|"
  139.          "for\\(\\|_\\(array_keys\\|file\\|recursive_glob\\)\\|each\\)\\|"
  140.          "i\\([fn]\\|tcl_class\\)\\|loop\\|proc\\|return\\|switch\\|"
  141.          "then\\|uplevel\\|while"
  142.          "\\)\\>")
  143.      ;;
  144.      ;; Types.
  145. ;   (make-regexp '("global" "upvar" "inherit" "public" "protected" "common"))
  146.      (cons (concat "\\<\\(common\\|global\\|inherit\\|"
  147.            "p\\(rotected\\|ublic\\)\\|upvar\\)\\>")
  148.        'font-lock-type-face)
  149.      ))
  150.   "Default expressions to highlight in TCL modes.")
  151.  
  152. (defvar tcl-imenu-generic-expression
  153.   '((nil "^[ \t]*proc[ \t]+\\(\\(\\s_\\|\\sw\\)+\\)" 1))
  154.   "Imenu generic expression for tcl-mode.  See `imenu-generic-expression'.")
  155.  
  156.  
  157. ;;}}}
  158. ;;{{{ tcl-mode
  159.  
  160. ;;;###autoload
  161. (defun tcl-mode ()
  162.   "Major mode for editing tcl scripts.
  163. The following keys are bound:
  164. \\{tcl-mode-map}
  165. "
  166.   (interactive)
  167.   (let ((switches nil)
  168.     s)
  169.     (kill-all-local-variables)
  170.     (setq major-mode 'tcl-mode)
  171.     (setq mode-name "TCL")
  172.     (set (make-local-variable 'tcl-process) nil)
  173.     (set (make-local-variable 'tcl-process-buffer) nil)
  174.     (make-local-variable 'tcl-default-command-switches)
  175.     (set (make-local-variable 'indent-line-function) 'tcl-indent-line)
  176.     (set (make-local-variable 'comment-start) "# ")
  177.     (set (make-local-variable 'comment-start-skip) "# *")
  178.     (set (make-local-variable 'font-lock-defaults)
  179.      '(tcl-font-lock-keywords nil nil ((?_ . "w"))))
  180.     (set (make-local-variable 'imenu-generic-expression)
  181.      tcl-imenu-generic-expression)
  182.     (make-local-variable 'tcl-default-eval)
  183.     (or tcl-mode-map
  184.     (tcl-setup-keymap))
  185.     (use-local-map tcl-mode-map)
  186.     (set-syntax-table (copy-syntax-table))
  187.     (modify-syntax-entry ?# "<")
  188.     (modify-syntax-entry ?\n ">")
  189.     ;; look for a #!.../wish -f line at bob
  190.     (save-excursion
  191.       (goto-char (point-min))
  192.       (if (looking-at "#![ \t]*\\([^ \t]*\\)[ \t]\\(.*[ \t]\\)-f")
  193.       (progn
  194.         (set (make-local-variable 'tcl-default-application)
  195.          (buffer-substring (match-beginning 1)
  196.                    (match-end 1)))
  197.         (if (match-beginning 2)
  198.         (progn
  199.           (goto-char (match-beginning 2))
  200.           (set (make-local-variable 'tcl-default-command-switches) nil)
  201.           (while (< (point) (match-end 2))
  202.             (setq s (read (current-buffer)))
  203.             (if (<= (point) (match-end 2))
  204.             (setq tcl-default-command-switches
  205.                   (append tcl-default-command-switches
  206.                       (list (prin1-to-string s)))))))))
  207.     ;; if this fails, look for the #!/bin/csh ... exec hack
  208.     (while (eq (following-char) ?#)
  209.       (forward-line 1))
  210.     (or (bobp)
  211.         (forward-char -1))
  212.     (if (eq (preceding-char) ?\\)
  213.         (progn
  214.           (forward-char 1)
  215.           (if (looking-at "exec[ \t]+\\([^ \t]*\\)[ \t]\\(.*[ \t]\\)*-f")
  216.           (progn
  217.             (set (make-local-variable 'tcl-default-application)
  218.              (buffer-substring (match-beginning 1)
  219.                        (match-end 1)))
  220.             (if (match-beginning 2)
  221.             (progn
  222.               (goto-char (match-beginning 2))
  223.               (set (make-local-variable
  224.                 'tcl-default-command-switches)
  225.                    nil)
  226.               (while (< (point) (match-end 2))
  227.                 (setq s (read (current-buffer)))
  228.                 (if (<= (point) (match-end 2))
  229.                 (setq tcl-default-command-switches
  230.                       (append tcl-default-command-switches
  231.                           (list (prin1-to-string s)))))))))
  232.         )))))
  233.     (run-hooks 'tcl-mode-hook)))
  234.  
  235. ;;}}}
  236. ;;{{{ tcl-setup-keymap
  237.  
  238. (defun tcl-setup-keymap ()
  239.   "Set up keymap for tcl mode.
  240. If the variable `tcl-prefix-key' is nil, the bindings go directly
  241. to `tcl-mode-map', otherwise they are prefixed with `tcl-prefix-key'."
  242.   (setq tcl-mode-map (make-sparse-keym